package com.hzjh.h5mall.model.wallet

import androidx.lifecycle.MutableLiveData
import com.hzjh.base.ext.R
import com.hzjh.base.ext.data.UserDataBean
import com.hzjh.base.ext.frame.JHBaseVM
import com.hzjh.base.ext.http.HttpConfig
import com.hzjh.base.http.toParams
import com.hzjh.base.http.toVersionParams
import com.hzjh.base.utils.function.toJson
import com.hzjh.base.utils.other.CommUtils
import com.hzjh.h5mall.BuildConfig
import com.hzjh.h5mall.bean.WalletProcessStatus
import com.hzjh.h5mall.bean.wallet.PayInfoBean
import com.hzjh.h5mall.bean.wallet.RechargeMoneyItem
import com.hzjh.h5mall.bean.wallet.WalletBean
import com.hzjh.h5mall.bean.wallet.WalletClassBean
import com.hzjh.h5mall.bean.wallet.WalletDetailBean
import com.hzjh.h5mall.bean.wallet.WalletProcessBean
import com.hzjh.h5mall.http.WalletApi
import com.hzjh.service.metamask.MetaMaskUtils

class WalletVM : JHBaseVM() {
    val classBalanceList:MutableLiveData<ArrayList<WalletClassBean>> = MutableLiveData(ArrayList())
    val rechargeMoneyList:MutableLiveData<ArrayList<RechargeMoneyItem>> = MutableLiveData(ArrayList())
    val processList:MutableLiveData<ArrayList<WalletProcessBean>> = MutableLiveData(ArrayList())
    val walletBean:MutableLiveData<WalletBean> = MutableLiveData()
    val userInfoBean:MutableLiveData<UserDataBean> = MutableLiveData()
    val payInfoBean:MutableLiveData<PayInfoBean> = MutableLiveData() //支付信息
    val addPayIdResult:MutableLiveData<String> = MutableLiveData() //充值结果
    val withdrawCount:MutableLiveData<Int> = MutableLiveData() //提现次数
    val withdrawResult:MutableLiveData<String> = MutableLiveData() //提现成功
    val walletDetailBean:MutableLiveData<WalletDetailBean> = MutableLiveData()//钱包明细
    var metamaskRechargeMoney:String?=null
    var ethRate:MutableLiveData<String> = MutableLiveData()//eth汇率

    //初始化MetaMask
    fun initMetaMask(){
        MetaMaskUtils.init(CommUtils.getString(com.hzjh.h5mall.R.string.app_name), BuildConfig.h5Url)
    }

    //收益类型
    fun getClassBalanceList(profitMoney:String?,adMoney:String?,boxMoney:String?){
        classBalanceList.value?.clear()
        classBalanceList.value?.add(WalletClassBean(R.mipmap.icon_profitshare_tag,profitMoney?:"0.00",CommUtils.getString(R.string.jh_ProfitShare),"profit"))
        classBalanceList.value?.add(WalletClassBean(R.mipmap.icon_ad_tag,adMoney?:"0.00",CommUtils.getString(R.string.jh_AdRevenue),"ad"))
        classBalanceList.value?.add(WalletClassBean(R.mipmap.icon_blindbox_tag,boxMoney?:"0.00",CommUtils.getString(R.string.jh_BlindBoxSale),"blindbox"))
        classBalanceList.postValue(classBalanceList.value)
    }
    //获取充值金额列表
    fun getRechargeMoneyList(){
        rechargeMoneyList.value?.add(RechargeMoneyItem(50,"50"))
        rechargeMoneyList.value?.add(RechargeMoneyItem(100,"100"))
        rechargeMoneyList.value?.add(RechargeMoneyItem(200,"200"))
        rechargeMoneyList.value?.add(RechargeMoneyItem(500,"500"))
        rechargeMoneyList.value?.add(RechargeMoneyItem(1000,"1K"))
        rechargeMoneyList.value?.add(RechargeMoneyItem(2000,"2K"))
    }
    //获取进度
    fun getProcessList(item: WalletDetailBean){
        processList.value?.add(WalletProcessBean(CommUtils.getString(R.string.jh_review_submitted),item.createTime?:"",WalletProcessStatus.COMPLETE))
        processList.value?.add(WalletProcessBean(CommUtils.getString(R.string.jh_pending_approval),item.auditTime?:"",if(item.status==0) WalletProcessStatus.PROCESSING else WalletProcessStatus.COMPLETE))
        processList.value?.add(WalletProcessBean(
            if(item.status!=-1) CommUtils.getString(R.string.jh_Done) else CommUtils.getString(R.string.jh_Failed),
            if(item.status==0) "" else item.updateTime?:"", //没有审核结果时不显示时间
            if(item.status==0) WalletProcessStatus.NOT_START else if(item.status==1) WalletProcessStatus.COMPLETE else WalletProcessStatus.FAIL ,
            item.mark?:""))
        processList.postValue(processList.value)
    }

    //获取我的钱包数据
    fun reqMyWallet(){
        val params = HashMap<String, String>()
        makeCall {
            onRequest {
                WalletApi.create().reqMyWallet(params)
            }

            onSuccess {
                walletBean.value=it
            }

        }
    }
    //绑定metamask账号
    fun reqAddMetaMask(metamastNo:String){
        makeCall {
            onRequest {
                WalletApi.create().reqAddMetaMask(metamastNo)
            }

            onRealSuccess {
                //绑定成功
                reqMyWallet()
            }

        }
    }
    //删除metamask账号
    fun reqDeleteMetaMask(metamastNo:String){
        makeCall {
            onRequest {
                WalletApi.create().reqDelMetaMask(metamastNo)
            }
            onRealSuccess {
                //删除成功
                reqMyWallet()
            }
        }
    }

    //用户信息接口
    fun reqUserInfo(){
        val params = HashMap<String, String>()
        makeCall {
            onRequest {
                WalletApi.create().reqUserInfo(params)
            }
            onSuccess {
                userInfoBean.value=it
            }
        }
    }
    //获取充值二维码信息
    fun reqRechargeQrCode(){
        val params = HashMap<String, String>()
        makeCall {
            onRequest {
                WalletApi.create().reqRechargeQrCode(params)
            }
            onSuccess {
                payInfoBean.value=it
            }
        }
    }

    //支付
    fun reqPay(address:String,money:String){
        MetaMaskUtils.getInstance()?.reqSendTransaction(address,money,
            success = {
                //调提交交易码
                CommUtils.getHandler().postDelayed({
                    reqAddPayId(it, payType = "metamask", money = metamaskRechargeMoney)
                },1000)
            },
            fail = {
                //充值失败
                CommUtils.getHandler().postDelayed({
                    addPayIdResult.value=""
                },1000)
            })
    }
    //提交交易码
    fun reqAddPayId(payId:String,payType:String?=null,money: String?=null){
        val params = HashMap<String, String>()
        params["payId"] = payId
        params["type"] = "1" //充值type=1
        payType?.let{
            params["payType"] = it
        }
        money?.let{
            params["money"] = it
        }
        makeCall {
            onRealStart {
                //加密
                HttpConfig.addHeaders(CommUtils.getMD5(CommUtils.getMapAscii(params) + HttpConfig.SignKey))
            }
            onRequest {
                WalletApi.create().reqAddPayId(params.toJson().toParams())
            }
            onSuccess {
                addPayIdResult.value=it
            }

            onRealComplete {
                //取消加密
                HttpConfig.addHeaders()
            }
        }
    }
    //获取提现次数
    fun reqWithdrawNum(){
        val params = HashMap<String, String>()
        makeCall {
            onRequest {
                WalletApi.create().reqWithdrawCount(params)
            }
            onSuccess {
                withdrawCount.value=it
            }
        }
    }
    //申请提现
    fun reqWithdraw(metamastNo:String,money:String){
        val params = HashMap<Any, Any>()
        params["payId"] = metamastNo
        params["money"] = money
        params["type"]=0 //提现type=0
        makeCall {
            onRequest {
                WalletApi.create().reqWithdraw(params.toVersionParams())
            }
            onSuccess {
                withdrawResult.value=it
            }
        }
    }
    //获取账单明细
    fun reqWalletDetail(id:String){
        makeCall {
            onRequest {
                WalletApi.create().reqWalletDetail(id)
            }
            onSuccess {
                walletDetailBean.value=it
            }
        }
    }
    //获取比特币汇率
    fun reqEthRate(){
        makeCall {
            onRequest {
                WalletApi.create().reqEthRate()
            }
            onSuccess {
                ethRate.value=it
            }
        }
    }
}